const string = `It is said that the apple hit Newton's head rendered the enlightment of gravity but maybe it's just a made-up story. His thoughts on "why should that apple always descend perpendicularly to the ground, not go sideways nor upwards but constantly to the earth's center" might have persisted for a very long time.`;

// 通过计分来来完成比较
function calculate(string) {
    let score = 10;

    score += string.length;

    return score;
}

/**
 * @param {string} alinz 
 * @return {string}
 */
function game(alinz) {
    const alinzArray = alinz.match(/[A-Za-z'-]+/g);
    const result = {};
    alinzArray.forEach(p => {
        
        const firstString = p.slice(0,1).toUpperCase();
        const string = firstString + p.slice(1, p.length);

        if (!result[firstString]) { 
            result[firstString] = string;
            return;
        }
        
        const resultNumber = calculate(result[firstString]) - calculate(string);

        if (resultNumber < 0) {
            result[firstString] = string;
        }
    });

    let resultString = "";
    Object.keys(result).sort().forEach(p => {
        resultString += result[p];
    })

    return resultString;
}

const rightAnswers = "AlwaysButConstantlyDescendEnlightmentForGravityHeadIt'sJustLongMade-upNewton'sOfPerpendicularlyRenderedSidewaysThoughtsUpwardsVeryWhy";

console.log(game(string) === rightAnswers); 

